// templates/warehouse/pages/warehouse/index.js
import Notify from '@vant/weapp/notify/notify';
import Dialog from '@vant/weapp/dialog/dialog';
import { clearUserLocalStorage, userRequest, formatPrice } from "../../../../utils/util";
const app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    orders: [],
    refund_type_id: 0,
    refund_type_list: [
      { text: '换货', value: '换货' },
      { text: '退货退款', value: '退货退款' },
      { text: '仅退款', value: '仅退款' }
    ],



  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let now = (new Date().getTime()) / 1000;
    let group = wx.getStorageSync('role')
    let expiration_time = app.session.expiration_time ?? 0
    // 调用检查登录状态的函数
    this.checkLoginStatus(now, expiration_time, group, level);
    console.log('session:', app.session)
    // console.log(app.session.warehouse.delivery_man_id)
    let access_token = app.session.access_token
    console.log(access_token)
    let level = wx.getStorageSync('level')
    let current_delivery_man_id = app.session.user.delivery_man_id
    let current_delivery_district_id = app.session.user.delivery_district_id

    this.setData({
      'access_token': access_token,
      'current_delivery_district_id': current_delivery_district_id,
      'level': level,
      'current_delivery_man_id': current_delivery_man_id,
      'current_delivery_district_id': 0,
    })
    this.get_waiting_delivery()

  },
  checkLoginStatus(now, expiration_time, group, level) {
    if (group !== '仓库库管' && level !== '2' || now > expiration_time) {
      let groupname = group !== '仓库库管' ? '非仓库库管角色！' : '已过登录有效期!'
      wx.showToast({
        title: groupname,
        icon: 'error',
        duration: 2000
      })
      // console.log(group !== '仓库库管' ? '非仓库库管角色！' : '已过登录有效期!');
      app.session = {};
      clearUserLocalStorage();
      wx.reLaunch({
        url: '/pages/selectID/selectID',
      });
    }
  },
  async get_waiting_delivery() {

    const that = this
    let { access_token } = that.data
    console.log(access_token)
    const header = {
      'Content-Type': 'application/json',
      "x-api-key": access_token
    }
    const url = app.globalData.API + '/delivery/index/refund-orders';
    const data = {
      // current_delivery_district_id: that.data.current_delivery_district_id,
      // current_delivery_man_id: that.data.current_delivery_man_id,
      // current_delivery_delivery_district_id: 0,
    }
    try {
      // 显示加载提示
      wx.showLoading({
        title: '加载中...',
      });
      const res = await userRequest(url, 'POST', data, header);
      console.log(res)
      wx.hideLoading();

      if (res.code == 200) {
        let orders = res.data.orders
        that.setData({
          orders: orders
        })
        Notify({
          type: 'success',
          message: '数据加载完成'
        });
      }

    } catch (error) {
      console.error('请求失败:', error);
    }
    // wx.request({
    //   url: app.globalData.API + '/delivery/index/refund-orders',
    //   header: {
    //     'Content-Type': 'application/json',
    //     "x-api-key": this.data.access_token,
    //   },
    //   method: "POST",
    //   data: {
    //     current_delivery_district_id,
    //     current_delivery_man_id,
    //     current_delivery_delivery_district_id
    //   },
    //   // success(res) {

    //   // }
    // })

  },
  getafterorder() {
    wx.navigateTo({
      url: '/templates/warehouse/pages/afterorderinfo/afterorderinfo',
      success: function (res) {
        console.log('跳转成功');
      },
      fail: function (err) {
        console.log('跳转失败', err);
      }
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})